package com.example.demo.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.order.dto.request.CartAddRequest;
import com.example.demo.order.dto.request.CartBatchRequest;
import com.example.demo.order.dto.request.CartCheckoutRequest;
import com.example.demo.order.dto.request.CartUpdateRequest;
import com.example.demo.order.dto.request.OrderCreateRequest;
import com.example.demo.order.dto.request.PaymentRequest;
import com.example.demo.order.dto.response.CartResponse;
import com.example.demo.order.dto.response.OrderResponse;
import com.example.demo.order.dto.response.PaymentResponse;
import com.example.demo.common.Result;
import com.example.demo.order.service.CartService;
import com.example.demo.order.service.OrderService;
import com.example.demo.order.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

/**
 * @ClassName OrderController
 * @Description 订单管理
 * @Author MingChang.Wei
 * @Date 2025/8/8
 **/
@RestController
@RequestMapping("/order")
@Tag(name = "订单管理", description = "订单相关操作接口")
public class OrderController {

    @Resource
    private OrderService orderService;
    
    @Resource
    private CartService cartService;
    
    @Resource
    private PaymentService paymentService;

    // ==================== 订单相关接口 ====================
    
    @PostMapping("/create")
    @Operation(summary = "创建订单", description = "用户创建订单接口")
    public OrderResponse createOrder(@RequestBody @Valid OrderCreateRequest request) {
        return orderService.createOrder(request);
    }

    @PostMapping("/pay/{orderNo}")
    @Operation(summary = "支付订单", description = "用户支付订单接口")
    public boolean payOrder(@PathVariable String orderNo) {
        return orderService.payOrder(orderNo);
    }
    
    @PostMapping("/payment/initiate")
    @Operation(summary = "发起支付", description = "发起支付请求接口")
    public Result<PaymentResponse> initiatePayment(@RequestBody @Valid PaymentRequest request) {
        return Result.success(paymentService.initiatePayment(request));
    }
    
    @PostMapping("/payment/callback/{orderNo}")
    @Operation(summary = "支付回调", description = "第三方支付回调接口")
    public Result<Boolean> paymentCallback(@PathVariable String orderNo, @RequestParam String result) {
        return Result.success(paymentService.handlePaymentCallback(orderNo, result));
    }
    
    @GetMapping("/payment/status/{orderNo}")
    @Operation(summary = "查询支付状态", description = "查询订单支付状态接口")
    public Result<String> getPaymentStatus(@PathVariable String orderNo) {
        return Result.success(paymentService.getPaymentStatus(orderNo));
    }

    @PostMapping("/refund/{orderNo}")
    @Operation(summary = "退款订单", description = "用户退款订单接口")
    public boolean refundOrder(@PathVariable String orderNo) {
        return orderService.refundOrder(orderNo);
    }

    @GetMapping("/{orderNo}")
    @Operation(summary = "查询订单", description = "查询订单详情接口")
    public OrderResponse getOrder(@PathVariable String orderNo) {
        return orderService.getOrder(orderNo);
    }
    
    @GetMapping("/list/{username}")
    @Operation(summary = "查询用户订单列表", description = "分页查询用户订单列表")
    public Result<Page<OrderResponse>> getOrderList(
            @PathVariable String username,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(orderService.getOrderList(username, current, size));
    }

    // ==================== 购物车相关接口 ====================
    // 注意：所有购物车操作都基于当前登录用户（Sa-Token Session）
    // Request中的username字段已废弃，系统自动从Session获取当前用户
    
    @PostMapping("/cart/add")
    @Operation(summary = "添加商品到购物车", description = "添加商品到当前用户的购物车")
    public Result<Boolean> addToCart(@RequestBody @Valid CartAddRequest request) {
        return Result.success(cartService.addToCart(request));
    }
    
    @GetMapping("/cart/list")
    @Operation(summary = "查询购物车列表", description = "查询当前登录用户的购物车商品列表")
    public Result<Page<CartResponse>> getCartList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(cartService.getCartList(current, size));
    }
    
    @PutMapping("/cart/update")
    @Operation(summary = "更新购物车商品数量", description = "更新当前用户购物车中商品的数量")
    public Result<Boolean> updateCartQuantity(@RequestBody @Valid CartUpdateRequest request) {
        return Result.success(cartService.updateCartQuantity(request));
    }
    
    @DeleteMapping("/cart/{cartId}")
    @Operation(summary = "删除购物车商品", description = "删除当前用户购物车中的指定商品")
    public Result<Boolean> removeFromCart(@PathVariable Long cartId) {
        return Result.success(cartService.removeFromCart(cartId));
    }
    
    @DeleteMapping("/cart/batch")
    @Operation(summary = "批量删除购物车商品", description = "批量删除当前用户购物车中的商品")
    public Result<Boolean> batchRemoveFromCart(@RequestBody @Valid CartBatchRequest request) {
        return Result.success(cartService.batchRemoveFromCart(request.getCartIds(), request.getUsername()));
    }
    
    @PostMapping("/cart/checkout")
    @Operation(summary = "购物车结算", description = "将当前用户选中的购物车商品生成订单")
    public Result<List<OrderResponse>> checkoutCart(@RequestBody @Valid CartCheckoutRequest request) {
        return Result.success(cartService.checkoutCart(request));
    }
}
